package com.example.threaddemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_extends_start;

    private Button btn_imp_runable;

    private Button btn_anonymous_inner_class;
    //实现线程方法1，继承Thread类
    private class MythreadExtends extends Thread {

        private int ticket = 100; //100张票
        private String name;      //取票窗口
        private String TAG = "cxp";

        public MythreadExtends(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            while (ticket > 0){
                ticket --;
                Log.e(TAG, "run: " + name + "卖掉了一张票" + "剩余" + ticket);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private class MyThreadImpRunable implements Runnable{
        private int ticket = 100;
        private String name;
        private String TAG = "MyThreadImpRunable";

        public MyThreadImpRunable(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            while (ticket > 0){
                ticket --;
                Log.e(TAG, "run: " + name + "卖掉了一张票，剩余票数" +ticket );
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        btn_extends_start = findViewById(R.id.btn_extends_thread);
        btn_imp_runable = findViewById(R.id.btn_imp_runable);
        btn_anonymous_inner_class = findViewById(R.id.btn_anonymous_inner_class);

        btn_extends_start.setOnClickListener(this);
        btn_imp_runable.setOnClickListener(this);
        btn_anonymous_inner_class.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_extends_thread:
                MythreadExtends rob1 = new MythreadExtends("窗口1");
                MythreadExtends rob2 = new MythreadExtends("窗口2");
                rob1.start();
                rob2.start();
                break;
            case R.id.btn_imp_runable:
                MyThreadImpRunable run1 = new MyThreadImpRunable("窗口1");
                MyThreadImpRunable run2 = new MyThreadImpRunable("窗口2");
                Thread mythread1 = new Thread(run1);
                Thread mythread2 = new Thread(run2);
                mythread1.start();
                mythread2.start();
                break;
            case R.id.btn_anonymous_inner_class:
                new Thread(new Runnable() {
                    private String TAG = "Anonymous Inner Class";
                    private int ticket = 100;
                    private String name = "窗口1";
                    @Override
                    public void run() {
                        while (ticket > 0){
                            ticket --;
                            Log.e(TAG, "run: " + name + "卖掉了1张票，剩余票数"+ ticket);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();

                new Thread(new Runnable() {
                    private String TAG = "Anonymous Inner Class";
                    private int ticket = 100;
                    private String name = "窗口2";
                    @Override
                    public void run() {
                        while (ticket > 0){
                            ticket --;
                            Log.e(TAG, "run: " + name + "卖掉了1张票，剩余票数"+ ticket);
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            default:
                break;
        }
    }
}

